package com.bmh.two;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;

/**
 * Created by 10312 on 2017/3/15.
 */

public class WelcomeActivity extends Activity {

    private static final String url="https://raw.githubusercontent.com/wsf5918/sufun-test/master/";

    private String bmh_url;

    private long start_time;
    private long during_time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        start_time = System.currentTimeMillis();
        initView();
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Intent intent = null;
            intent = new Intent(WelcomeActivity.this, MainActivity.class);
            if(msg.what ==100){
                intent.putExtra("url", bmh_url);
            }
            startActivity(intent);
            finish();
        }
    };

    private void initView() {
        //获取channelId
        ApplicationInfo appInfo = null;
        try {
            appInfo = this.getPackageManager()
                    .getApplicationInfo(getPackageName()
                            , PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        final String channelId = appInfo.metaData.getString("CHANNEL_NAME");

        Log.i("", "--------->channelId="+channelId);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        IChoose choose = retrofit.create(IChoose.class);
        Call<ChooseModel> call = choose.getChoose();
        call.enqueue(new Callback<ChooseModel>() {
            @Override
            public void onResponse(Call<ChooseModel> call, Response<ChooseModel> response) {
                ChooseModel cm = response.body();
                for (ChannelInfo cf : cm.getUrls()){
                    Log.i("", "--------->getChannel="+cf.getChannel());
                    if(cf.getChannel().equals(channelId)){
                        bmh_url = cf.getUrl();
                        break;
                    }
                }
                during_time = System.currentTimeMillis() - start_time;
                Log.i("", "-------------->during_time= "+during_time);

                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                            mHandler.sendEmptyMessage(100);
                    }
                }, during_time > 1000 ? 0 : 1000);
            }

            @Override
            public void onFailure(Call<ChooseModel> call, Throwable t) {
                Log.i("", "----------->response.throwable="+t.getMessage());
            }
        });
    }

    public interface IChoose{
        @GET("applist.txt")
        Call<ChooseModel> getChoose();
    }
}
